package com.rzico.quartz.job;

import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Supplier;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.SupplierService;
import com.rzico.core.entity.SysJob;
import com.rzico.core.service.SysJobService;
import com.rzico.order.entity.Shipping;
import com.rzico.order.plugin.JDPluginService;
import com.rzico.order.service.ShippingService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 京东商品提交订单
 */
public class JDOrderSubmitTask implements Job {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected SysJobService sysJobService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private ShippingService shippingService;

    @Autowired
    protected JDPluginService jDPluginService;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String jobId = jobExecutionContext.getTrigger().getKey().getName();
        SysJob sysJob = sysJobService.selectByPrimaryKey(jobId);
        try {
            if (sysJob.getExecStatus().equals(1)) {
                //不能重复执行
                return;
            }
            sysJob.setExecStatus(1);
            sysJob.setJobDesc("开始执行:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);

            // 这里开始

            //获取商户号
            String mchId = "";
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("name","葫芦派");
            List<Enterprise> enterpriseList= enterpriseService.selectList(param);
            if (0 < enterpriseList.size()) {
                mchId = enterpriseList.get(0).getMchId();
            }
            //获取京东供应商id
            Long supplierId = 0L;
            Long enterpriceId = 0L;
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("name","京东");
            List<Supplier> supplierList = supplierService.selectList(params);
            if (0 < supplierList.size()) {
                supplierId = supplierList.get(0).getId();
                enterpriceId = supplierList.get(0).getEnterpriseId();
            }
            /**
             * 第一步，查询京东供应商,未发货的的发货单，判断该发货单是否已提交京东订单，
             * 未提交京东订单的发货单查询出发货单项，根据发货单总金额判断当前公司金额是否充足，充足进行下单，不足的暂不下单，发送消息提醒
             */
            Map<String,Object> shipParams = new HashMap<>();
            shipParams.put("supplierId", supplierId);
            shipParams.put("shippingStatus", 1);
            List<Shipping> shipList = shippingService.selectList(shipParams);
            jDPluginService.jdOrderSubmit(shipList, mchId);

            // end

            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行成功:"+sdf.format(new Date())+",下次执行:"+sdf.format(jobExecutionContext.getNextFireTime()));
            sysJobService.updateByPrimaryKeySelective(sysJob);
        }
        catch (Exception e) {
            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行错误:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);
        }

    }
}
